/**
 * Project: shuyin-sync
 * 
 * File Created at Mar 13, 2012
 * $Id$
 * 
 * Copyright 2008 6677bank.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * 6677bank Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with 6677bank.com.
 */
package com.bank6677.sync.csm.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.bank6677.common.Constants;
import com.bank6677.common.exception.BusinessException;
import com.bank6677.sync.csm.dao.UserDao;
import com.bank6677.sync.csm.domain.User;
import com.bank6677.sync.csm.domain.old.AdminInfo;
import com.bank6677.sync.csm.helper.UMUserHelper;
import com.bank6677.sync.csm.service.UserSyncService;
import com.bank6677.uambank.model.role.Role;
import com.bank6677.uambank.model.user.UserLogin;

/**
 * 新老系统业务用户数据同步实现
 * 
 * @author liyue
 * @version UserSyncServiceImpl.java Mar 13, 2012 5:23:03 PM
 */
@Service
public class UserSyncServiceImpl implements UserSyncService {

    @Autowired
    private UserDao user;
    
    private static Logger LOG = Logger.getLogger(UserSyncServiceImpl.class);

    @Override
    @Transactional(value = "uamTransactionManager", rollbackFor = BusinessException.class)
    public void syncCsmUsers() throws SQLException {
        List<AdminInfo> oldUsers = user.findAllUsersFromOld();
        List<User> umUsers = new ArrayList<User>();
        for (AdminInfo oldUser : oldUsers) {
            
            User umUser = copyAdminInfo2User(oldUser);
            umUser.setSystemId(Constants.CSM_USER_TAG);

            Role role = UMUserHelper.getUmRoleByOldRoleId(CollectionUtils.isEmpty(oldUser
                    .getRules()) ? null : oldUser.getRules().get(0).getId());

            if (role == null) {
                continue;//如果改用户无角色则不同步
            }

            List<Role> roles = new ArrayList<Role>();
            roles.add(role);
            umUser.setRoles(roles);

            umUsers.add(umUser);
        }

        try {
            user.batchInsert(umUsers);
        } catch (SQLException err) {
            LOG.error("批量插入用户时出错！", err);
//            throw new BusinessException("批量插入用户时出错！", err);
        }
    }

    /**
     * 把老用户转为新用户实体
     * 
     * @param oldUser
     * @return
     */
    private User copyAdminInfo2User(AdminInfo oldUser) {
        User user = new User();
        user.setId(oldUser.getId());
        user.setUserName(oldUser.getAdminName());
        user.setUserPwd(UserLogin.encryptUserPwd(oldUser.getAdminName(), oldUser.getAdminPwd()));
        user.setEmail(oldUser.getEmail());
        user.setPhone(oldUser.getPhone());
        user.setSex(UMUserHelper.getSex(oldUser.getSex()));
        user.setRealName(oldUser.getRealName());
        user.setStatus(UMUserHelper.getStatus(oldUser.getStatus()));

        return user;
    }

}
